local skel = fk.CreateSkill{
  name = "ftk_room_trigger",
}
local U = require "packages/poker-games/utility"
Fk:loadTranslationTable{
  ["ftk_room_trigger"] = "FTK",
  [":ftk_room_trigger"] = "FTK",
  ["FTKround_rank-round"] = "已出完",
  ["@FTKrank-round"] = "本局排名",
  ["#ftk_response-invoke"] = "%src（%arg） 出了 %arg2，轮到你响应了！",

  ["ally_seat"] = "队友",
  ["next_seat"] = "下家",
  ["last_seat"] = "上家",
}

---@type TrigSkelSpec<UseCardFunc>
local ftk = {
  can_refresh = function (self, event, target, player, data)
    return player == target
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local to = player.next

    if player:isKongcheng() then
      local rank = room:getBanner("FTKround_rank-round") or {}
      table.insertIfNeed(rank, player)
      room:setBanner("FTKround_rank-round", rank)
      room:setPlayerMark(player, "@FTKrank-round", #rank)
    end

    room:setTag("ftk_current_winner", player)
    local current_cards = room:getBanner("@$ftk_current_cards") or {}
    for _, id in ipairs(Card:getIdList(data.card)) do
      table.insertIfNeed(current_cards, id)
    end
    room:setBanner("@$ftk_current_cards", current_cards)
    for i = 1, 3 do
      if not to:isKongcheng() then
        local location = "ally_seat"
        if to.next == player then
          location = "next_seat"
        elseif player.next == to then
          location = "last_seat"
        end
        local suc, dat = room:askToUseActiveSkill(to, {
          skill_name = "ftk_response",
          skip = false,
          extra_data = {
            to_cards = Card:getIdList(data.card)
          },
          prompt = "#ftk_response-invoke:" .. player.id .. "::" .. location .. ":" .. data.card:toLogString()
        })
        if suc and dat then
          local cids = dat.cards
          local c = Fk:cloneCard(dat.interaction)
          c:addSubcards(cids)
          room:useCard{
            from = to,
            tos = {},
            card = c
          }
          break
        else
          to = to.next
        end
      else
        to = to.next
      end
    end
  end
}
skel:addEffect(fk.CardUseFinished, ftk)
return skel